<template>
  <div id="globalHeader">

    <a-row :wrap="false">
      <a-col flex="200px">
        <router-link to="/">
          <div class="title-bar">
            <img src="../assets/logo.jpg" alt="logo" class="logo">
            <div class="title">云图库</div>
          </div>
        </router-link>
      </a-col>
      <a-col flex="auto">
        <a-menu v-model:selectedKeys="current" mode="horizontal" :items="items" @click="doMenuClick"/>
      </a-col>
      <a-col flex="120px">
<!--        用户信息显示-->
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">


            <a-dropdown>
              <a class="ant-dropdown-link" @click.prevent>
                <a-space>
                  <a-avatar :src="loginUserStore.loginUser.userAvatar"></a-avatar>
                  <div >{{ loginUserStore.loginUser.userName?? "无名"}}</div>
                </a-space>

              </a>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLogout">
                  退出登录 <LogoutOutlined/>
                  </a-menu-item>

                </a-menu>
              </template>
            </a-dropdown>

          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>




  </div>
</template>
<script lang="ts" setup>
import { computed, h, ref } from 'vue'
import { MailOutlined, AppstoreOutlined, LogoutOutlined } from '@ant-design/icons-vue';
import { MenuProps, message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'
import { userLogoutUsingPost } from '@/api/userController.ts'
const current = ref<string[]>(['mail']);
const originItems = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(MailOutlined),
    label: '首页',
    title: '首页',
  },
  {
    key: '/admin/userManage',
    icon: () => h(AppstoreOutlined),
    label: '用户管理',
    title: '用户管理',
  },

  {
    key: 'others',
    label: h('a', { href: 'https://antdv.com', target: '_blank' }, '其他'),
    title: '其他',
  },
]);


// 过滤菜单项
const filterMenus = (menus=[] as MenuProps['items'])=>{
  return  menus?.filter((menu)=>{
    if (menu?.key?.startsWith("/admin")){
      const loginUser = loginUserStore.loginUser
      if (!loginUser || loginUser.userRole !== "admin"){
        return false
      }
    }
    return true
  })
}

// 展示在菜单的路由数组
const items = computed(()=>{
  return filterMenus(originItems.value)
})

const router = useRouter()
// 监听路由，，刷新之后回显menu
router.afterEach((to,from,next)=>{
  console.log(to,from,next)
  current.value = [to.path]
})

// 路由跳转事件
const doMenuClick = ({key})=>{
    router.push({
      path:key
    })
}


const loginUserStore = useLoginUserStore()

const doLogout = async ()=>{
  const  res = await userLogoutUsingPost()
  if (res.data.code ===0){
    loginUserStore.setLoginUser({
      userName:"未登录"
    })

    message.success("退出登录成功")
    router.push("/user/login")
  }else{
    message.error("退出登录失败"+res.data.message)
  }

}

</script>


<style scoped>
#globalHeader .title-bar{
  display: flex;
  align-items: center;

}

.title{
  color: black;
  font-size: 18px;
  margin-left: 20px;
}

.logo{
  height: 48px;
}
</style>

